package com.fjb.service.user.impl;

import com.fjb.pojo.product.ProductInfo;
import com.fjb.pojo.product.vo.ProductInfoPageVo;
import com.fjb.pojo.user.SysUserCollect;
import com.fjb.pojo.user.vo.UserCollectVo;
import com.fjb.service.user.SysUserCollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonPagesResult;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.enums.product.DealType;
import com.fjb.common.enums.product.ProductStatus;
import com.fjb.common.enums.user.CollectType;
import com.fjb.common.global.GlobalConfig;
import com.fjb.dto.LoginUserInfo;
import com.fjb.mapper.product.ProductInfoMapper;
import com.fjb.mapper.user.SysUserCollectMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户收藏 服务实现类
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Service
public class SysUserCollectServiceImpl extends ServiceImpl<SysUserCollectMapper, SysUserCollect> implements SysUserCollectService {

	@Autowired
	private SysUserCollectMapper sysUserCollectMapper;
	@Autowired
	private ProductInfoMapper productInfoMapper;
	
	@Transactional
	@Override
	public JsonResult<Boolean> addProduct(LoginUserInfo userInfo, Integer productId) {
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		Integer userId = userInfo.getUserId();
		Integer mainUserId = userInfo.getMainUserId();
		ProductInfo oldProduct = productInfoMapper.selectById(productId);
		if(oldProduct==null) {
			httpCode.setMsg("产品信息不存在");
			return new JsonResult<Boolean>(null, httpCode);
		}
		Integer userId2 = oldProduct.getUserId();
		if(userId.equals(userId2)) {
			httpCode.setMsg("不可收藏自己");	
			return new JsonResult<Boolean>(null, httpCode);
		}
		
		SysUserCollect uc = new SysUserCollect();
		uc.setMainUserId(mainUserId);
		uc.setUserId(userId);
		uc.setCollectType(CollectType.TYPE_1.getCode());
		uc.setCreateUserId(userId);
		uc.setCreateTime(LocalDateTime.now());
		uc.setBusinessId(productId);
		int i = sysUserCollectMapper.insert(uc);
		if(i==0) {
			httpCode.setMsg("添加收藏失败,请联系管理员");
			return new JsonResult<Boolean>(null, httpCode);
		}
		productInfoMapper.updateCollectCountAdd(productId);
		return new JsonResult<Boolean>(null, HttpCode.SUCCESS);
	}
	
	@Transactional
	@Override
	public JsonResult<Boolean> deleteProduct(Integer productId, Integer userId) {
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		QueryWrapper<SysUserCollect> queryWrapper = new QueryWrapper<SysUserCollect>();
		queryWrapper.eq("user_id", userId);
		queryWrapper.eq("business_id", productId);
		int i = sysUserCollectMapper.delete(queryWrapper);
		if(i==0) {
			httpCode.setMsg("取消收藏失败,请联系管理员");
			return new JsonResult<Boolean>(null, httpCode);
		}	
		productInfoMapper.updateCollectCountReduce(productId);
		return new JsonResult<Boolean>(null, HttpCode.SUCCESS);
	}

	@Override
	public JsonPagesResult<List<UserCollectVo>> selectPageList(PageQuery pageQuery, Integer userId) {
		PageHelper.startPage(pageQuery.getCurrentPage(), pageQuery.getPageSize());
		List<UserCollectVo> infoList = sysUserCollectMapper.selectPageList(userId);
		PageInfo<UserCollectVo> pageInfo = new PageInfo<UserCollectVo>(infoList);
		if(infoList.size()>0) {
			for (UserCollectVo in : infoList) {
				// 主文件	
				String mainFileUrl = in.getMainFileUrl();
				if(StringUtils.isNoneBlank(mainFileUrl)) {
					in.setMainFileUrl(GlobalConfig.FILE_SERVICE_LOCATION+mainFileUrl);
				}	
				// 状态msg
				Integer status = in.getProductStatus();
				String statusMsg = ProductStatus.getStatusMsg(status);
				if(StringUtils.isNotBlank(statusMsg)) {
					in.setProductStatusMsg(statusMsg);
				}	
				// 交易类型msg
				Integer dealType = in.getDealType();
				String typeMsg = DealType.getTypeMsg(dealType);
				if(StringUtils.isNotBlank(typeMsg)) {
					in.setDealTypeMsg(typeMsg);	
				}
			}
		}
		return new JsonPagesResult<List<UserCollectVo>>(infoList, HttpCode.SUCCESS, pageInfo.getTotal(), pageInfo.getPages());
	}

}
